<?php
require 'helpers/Utils.php';
class lanye_wdModuleSite extends WeModuleSite
{
	use Utils;
	public function __construct()
	{
		global $_GPC, $_W;
		$userInfo = pdo_get('d1sj_card_user', ['uid' => $_W['fans']['uid'], 'uniacid' => $_W['uniacid']]);
		if ($userInfo['openid'] == '') {
			$data = ['openid' => $_W['openid']];
			pdo_update('d1sj_card_user', $data, ['id' => $userInfo['id']]);
		}
	}
	public function users($id)
	{
		global $_GPC, $_W;
		$users = pdo_fetch('select * from ' . tablename('mc_members') . "where uniacid=:uniacid and uid=:uid", array(':uid' => $id, ':uniacid' => $_W['uniacid']));
		return $users;
	}
	public function user($id)
	{
		global $_GPC, $_W;
		$user = pdo_fetch('select * from ' . tablename('d1sj_card_user') . "where uniacid=:uniacid and uid=:uid", array(':uid' => $id, ':uniacid' => $_W['uniacid']));
		return $user;
	}
	public function delFile($dirName)
	{
		if (is_dir($dirName)) {
			if ($handle = opendir("$dirName")) {
				while (false !== ($item = readdir($handle))) {
					if ($item != "." && $item != "..") {
						if (is_dir("$dirName/$item")) {
							del_DirAndFile("$dirName/$item");
						} else {
							if (unlink("$dirName/$item")) {
							}
						}
					}
				}
				closedir($handle);
			}
		}
	}
	public function bank($id)
	{
		global $_GPC, $_W;
		$bank = pdo_fetch('select * from ' . tablename('d1sj_card_bank') . "where uniacid=:uniacid and id=:id", array(':id' => $id, ':uniacid' => $_W['uniacid']));
		return $bank;
	}
	public function group($id)
	{
		global $_GPC, $_W;
		$group = pdo_fetch('select * from ' . tablename('d1sj_card_group') . "where uniacid=:uniacid and id=:id", array(':id' => $id, ':uniacid' => $_W['uniacid']));
		return $group;
	}
	public function lend($id)
	{
		global $_GPC, $_W;
		$group = pdo_fetch('select * from ' . tablename('d1sj_card_lend') . "where uniacid=:uniacid and id=:id", array(':id' => $id, ':uniacid' => $_W['uniacid']));
		return $group;
	}
	public function payResult($params)
	{
		global $_W;
		$uniacid = $_W['uniacid'];
		if ($params['result'] == 'success' && ($params['from'] == 'notify' || $params['from'] == 'return')) {
			$order = $this->getOrderBySN($params['tid']);
			if ($order) {
				pdo_update('d1sj_card_order', ['pay_time' => time(), 'status' => 1], ['id' => $order['id']]);
				pdo_update('d1sj_card_user', ['group' => $order['group']], ['uid' => $_W['fans']['uid'], 'uniacid' => $_W['uniacid']]);
				$config = $this->config();
				$groupSql = 'SELECT * FROM ' . tablename('d1sj_card_group') . ' WHERE uniacid = :uniacid AND id = :id';
				$group = pdo_fetch($groupSql, [':uniacid' => $uniacid, ':id' => $order['group']]);
				$userSql = 'SELECT * FROM ' . tablename('d1sj_card_user') . ' WHERE uniacid = :uniacid AND uid = :uid';
				$user = pdo_fetch($userSql, [':uniacid' => $uniacid, ':uid' => $_W['fans']['uid']]);
				$groupMoney_noe = $group['mout'];
				$groupMoney_tow = $group['mout_tow'];
				if ($user['leader1']) {
					$group1 = $this->get_group_info_by_id($user['leader1'], true);
					$groupLevel1 = $group1 ? $group1['mlevel'] : 0;
				}
				if ($user['leader2']) {
					$group2 = $this->get_group_info_by_id($user['leader2'], true);
					$groupLevel2 = $group2 ? $group2['mlevel'] : 0;
				}
				if ($user['leader1'] && $groupLevel1 >= 1) {
					$this->upUserMoney($user['leader1'], $groupMoney_noe);
					$data_arr['fan_uid1'] = $user['leader1'];
					$data_arr['fan_level1'] = $groupMoney_noe;
				}
				if ($user['leader2'] && $groupLevel2 >= 2) {
					$this->upUserMoney($user['leader2'], $groupMoney_tow);
					$data_arr['fan_uid2'] = $user['leader2'];
					$data_arr['fan_level2'] = $groupMoney_tow;
				}
				if ($data_arr) {
					$data_arr['uid'] = $user['uid'];
					$data_arr['uniacid'] = $_W['uniacid'];
					$data_arr['bank'] = $group['id'];
					$data_arr['type'] = 3;
					$data_arr['name'] = $user['name'];
					$data_arr['mobile'] = $user['mobile'];
					$data_arr['status'] = 1;
					$data_arr['fan_total'] = $data_arr['fan_level1'] + $data_arr['fan_level2'];
					$data_arr['createtime'] = time();
					pdo_insert('d1sj_card_record', $data_arr);
				}
				message('购买成功', $this->createMobileUrl('user'), 'success');
			}
		}
		message('支付失败，请重试');
	}
	public function getOrderBySN($sn)
	{
		$sql = 'SELECT * FROM ' . tablename('d1sj_card_order') . ' WHERE sn = :sn';
		$info = pdo_fetch($sql, [':sn' => $sn]);
		return $info;
	}
	public function config()
	{
		global $_W;
		$sql = 'SELECT * FROM ' . tablename('d1sj_card_setting') . ' WHERE uniacid = :uniacid';
		$res = pdo_fetch($sql, [':uniacid' => $_W['uniacid']]);
		$config = unserialize($res['config']);
		$config['invite'] = $res['invite'];
		return $config;
	}
	public function upUserMoney($uid, $money)
	{
		global $_W;
		$sql = 'UPDATE ' . tablename('d1sj_card_user') . ' SET money = money + ' . floatval($money) . ' WHERE uniacid = :uniacid AND uid = :uid';
		pdo_query($sql, [':uniacid' => $_W['uniacid'], ':uid' => $uid]);
	}
	public function tixian_muban($toopenid, $template_id, $money)
	{
		$access_token = $this->getAccessToken();
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
		$link = '';
		$arr = array("touser" => $toopenid, "template_id" => $template_id, "url" => $link, "data" => array("first" => array("value" => "网贷提现通知", "color" => '#173177'), "keyword1" => array("value" => "网贷", "color" => '#173177'), "keyword2" => array("value" => $money, "color" => "#173177"), "keyword3" => array("value" => "微信零钱", "color" => "#173177"), "keyword3" => array("value" => date('Y-m-d H:i:s', time()), "color" => "#173177"), "keyword4" => array("value" => "提现成功", "color" => "#173177"), "remark" => array("value" => "您的问题已经处理", "color" => "#173177")));
		$json = urldecode(json_encode($arr));
		$res = $this->curl($url, 2, $json);
		return $res;
	}
	public function member_muban($toopenid, $template_id, $yuan_grade, $dang_grade)
	{
		$access_token = $this->getAccessToken();
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
		$link = '';
		$arr = array("touser" => $toopenid, "template_id" => $template_id, "url" => $link, "data" => array("first" => array("value" => "网贷会员等级变更通知", "color" => '#173177'), "keyword1" => array("value" => $yuan_grade, "color" => '#173177'), "keyword2" => array("value" => $dang_grade, "color" => "#173177"), "keyword3" => array("value" => date('Y-m-d H:i:s', time()) . "开始", "color" => "#173177"), "remark" => array("value" => "感谢您的使用", "color" => "#173177")));
		$json = urldecode(json_encode($arr));
		$res = $this->curl($url, 2, $json);
		return $res;
	}
	public function member_to_muban($toopenid, $template_id, $yuan_grade, $dang_grade)
	{
		$access_token = $this->getAccessToken();
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
		$link = '';
		$arr = array("touser" => $toopenid, "template_id" => $template_id, "url" => $link, "data" => array("first" => array("value" => "您推荐的会员等级变更通知", "color" => '#173177'), "keyword1" => array("value" => $yuan_grade, "color" => '#173177'), "keyword2" => array("value" => $dang_grade, "color" => "#173177"), "keyword3" => array("value" => date('Y-m-d H:i:s', time()) . "开始", "color" => "#173177"), "remark" => array("value" => "感谢您的使用", "color" => "#173177")));
		$json = urldecode(json_encode($arr));
		$res = $this->curl($url, 2, $json);
		return $res;
	}
	public function to_examine_muban($toopenid, $template_id, $bian_name, $xiaoxi_id)
	{
		$access_token = $this->getAccessToken();
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
		$link = '';
		$arr = array("touser" => $toopenid, "template_id" => $template_id, "url" => $link, "data" => array("first" => array("value" => "网贷" . $bian_name, "color" => '#173177'), "keyword1" => array("value" => $xiaoxi_id, "color" => '#173177'), "keyword2" => array("value" => '恭喜您申请的' . $bian_name . '成功通过审核', "color" => "#173177"), "remark" => array("value" => "感谢您的使用", "color" => "#173177")));
		$json = urldecode(json_encode($arr));
		$res = $this->curl($url, 2, $json);
		return $res;
	}
	public function getAccessToken()
	{
		load()->classs('weixin.account');
		$accObj = WeixinAccount::create(37);
		$access_token = $accObj->fetch_token();
		return $access_token;
	}
	public function curl($url, $type = 1, $data = null)
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		if ($type == 2) {
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		}
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($curl, CURLOPT_TIMEOUT, 30);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$tmpInfo = curl_exec($curl);
		if (curl_errno($curl)) {
			return 'Error' . curl_error($curl);
		}
		curl_close($curl);
		return $tmpInfo;
	}
	public function is_idcard($id)
	{
		$id = strtoupper($id);
		$regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
		$arr_split = array();
		if (!preg_match($regx, $id)) {
			return FALSE;
		}
		if (15 == strlen($id)) {
			$regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
			@preg_match($regx, $id, $arr_split);
			$dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
			if (!strtotime($dtm_birth)) {
				return FALSE;
			} else {
				return TRUE;
			}
		} else {
			$regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
			@preg_match($regx, $id, $arr_split);
			$dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
			if (!strtotime($dtm_birth)) {
				return FALSE;
			} else {
				$arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
				$arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
				$sign = 0;
				for ($i = 0; $i < 17; $i++) {
					$b = (int) $id[$i];
					$w = $arr_int[$i];
					$sign += $b * $w;
				}
				$n = $sign % 11;
				$val_num = $arr_ch[$n];
				if ($val_num != substr($id, 17, 1)) {
					return FALSE;
				} else {
					return TRUE;
				}
			}
		}
	}
}